package com.example.flinktest.watermark;

import com.example.flinktest.source.ClickSource;
import com.example.flinktest.source.Event;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.time.Duration;

public class WatermarkTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.addSource(new ClickSource())
                //插入水位线的逻辑
                .assignTimestampsAndWatermarks(
                        //针对乱序流插入水位线，延迟时间设置为5秒
                        WatermarkStrategy.<Event>forBoundedOutOfOrderness(
                                Duration.ofSeconds(5)).withTimestampAssigner(
                                new SerializableTimestampAssigner<Event>() {
                                    //抽取时间戳
                                    @Override
                                    public long extractTimestamp(Event event, long l) {
                                        return event.timestamp;
                                    }
                                })
                ).print();
        env.execute();
    }
}
